package edu.wfu.preparedDishes.recipes.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;
import edu.wfu.preparedDishes.recipes.entity.RecipesEntity;
import edu.wfu.preparedDishes.recipes.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 食谱表;
 *
 * @author wdq
 * @date 2023-11-24 19:05:24
 */
@RestController
@RequestMapping("commodity/recipes")
public class RecipesController {
    @Autowired
    private RecipesService recipesService;

    /**
     * 随机获取菜谱信息
     */
    @RequestMapping("/random")
    public RecipesEntity randomGetRecipes() {
        List<RecipesEntity> list = recipesService.list();
        // 使用Random生成一个随机索引
        Random random = new Random();

        int randomIndex = random.nextInt(list.size());

        // 返回随机索引处的元素
        return list.get(randomIndex);

    }


    /**
     * 获取菜谱详情
     *
     * @return String 菜单详情的String 字符串
     * 数据有前端处理
     */

    @RequestMapping("/getRecipeDetail/{recipeId}")
    public String getRecipeDetail(@PathVariable String recipeId) {
        RecipesEntity byId = recipesService.getById(recipeId);
        return byId.getRecipesDetail();
    }

    /**
     * 返回某个用户的收藏列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = recipesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{recipesId}")
    public R info(@PathVariable("recipesId") String recipesId) {
        RecipesEntity recipe = recipesService.getById(recipesId);

        return R.ok().put("recipe", recipe);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map map) {
        String recipesId = (String) map.get("recipesId");
        String spuId = (String) map.get("spuId");
        Object recipesDetailObj = map.get("recipesDetail");
        String recipesDetail = null;

        if (recipesDetailObj instanceof String) {
            recipesDetail = (String) recipesDetailObj;
        } else if (recipesDetailObj != null) {
            // 处理其他情况，可能需要将对象转换为字符串
            ObjectMapper objectMapper = new ObjectMapper(); //用于处理JSON数据
            try {
                //将JSON数据转换成JSON字符串
                recipesDetail = objectMapper.writeValueAsString(recipesDetailObj);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }

        }
        RecipesEntity recipesEntity = new RecipesEntity();
        recipesEntity.setRecipesId(recipesId);
        recipesEntity.setSpuId(spuId);
        recipesEntity.setRecipesDetail(recipesDetail);
        recipesService.save(recipesEntity);

        return R.ok();
    }
/**
 * {mianSrc=../../images/commodity/aQiang.png,
 * mainText=爱跳舞的阿强, recipesDetail=[
 * {src=../../images/commodity/5.webp, text=第一步},
 * {src=../../images/commodity/5.webp, text=第二步},
 * {src=../../images/commodity/5.webp, text=第三步},
 * {src=../../images/commodity/5.webp, text=第四步},
 * {src=../../images/commodity/5.webp, text=第五步}]
 * }
 */

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RecipesEntity recipes) {
        recipesService.updateById(recipes);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] recipesIds) {
        recipesService.removeByIds(Arrays.asList(recipesIds));

        return R.ok();
    }

}
